//
//  YXPickerView.m
//  MockProject
//
//  Created by FanYu on 25/8/2016.
//  Copyright © 2016 niuzhaowang. All rights reserved.
//

#import "YXPickerView.h"

@interface YXPickerView () <UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) UIView *buttonView;
@property (nonatomic, strong) UIView *hairlineView;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *saveButton;

@end


@implementation YXPickerView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}


#pragma mark - Setup UI
- (void)setupUI {
    // self
    self.backgroundColor = [UIColor whiteColor];
    
    // content view
    self.buttonView = [[UIView alloc] init];
    self.buttonView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.buttonView];
    [self.buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.height.mas_equalTo(40);
    }];
    
    // cancel button
    self.cancelButton = [[UIButton alloc] init];
    self.cancelButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelButton setTitleColor:[UIColor colorWithHexString:@"403e3d"] forState:UIControlStateNormal];
    [self.buttonView addSubview:self.cancelButton];
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.buttonView.mas_left).offset(39);
        make.centerY.equalTo(self.buttonView.mas_centerY);
    }];
    [self.cancelButton addTarget:self action:@selector(cancelButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    // save button
    self.saveButton = [[UIButton alloc] init];
    self.saveButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.saveButton setTitle:@"保存" forState:UIControlStateNormal];
    [self.saveButton setTitleColor:[UIColor colorWithHexString:@"ff4600"] forState:UIControlStateNormal];
    [self.buttonView addSubview:self.saveButton];
    [self.saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.buttonView.mas_right).offset(-39);
        make.centerY.equalTo(self.buttonView.mas_centerY);
    }];
    [self.saveButton addTarget:self action:@selector(saveButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    // hairline
    self.hairlineView = [[UIView alloc] init];
    self.hairlineView.backgroundColor = [UIColor colorWithHexString:@"e0dcd9"];
    [self addSubview:self.hairlineView];
    [self.hairlineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.buttonView.mas_bottom);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.height.mas_equalTo(0.5);
    }];
    
    // pick view
    self.pickerView = [[UIPickerView alloc] init];
    self.pickerView.backgroundColor = [UIColor whiteColor];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    [self addSubview:self.pickerView];
    [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.hairlineView.mas_bottom);
        make.bottom.equalTo(self.mas_bottom);
        make.width.mas_equalTo(160);
        make.centerX.equalTo(self.mas_centerX);
    }];
    [self.pickerView selectRow:3 inComponent:0 animated:NO];
}


#pragma mark - Actions
- (void)saveButtonTapped:(UIButton *)sender {
    [self.delegate saveButtonTapped:sender];
}

- (void)cancelButtonTapped:(UIButton *)sender {
    [self.delegate cancelButtonTapped:sender];
}


#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return 9;
}

#pragma mark - UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    [self.delegate didSelectRow:row + 1];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return 100;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 40;
}

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSDictionary *textAttrs = @{
                                NSForegroundColorAttributeName : [UIColor colorWithHexString:@"ff4600"],
                                NSFontAttributeName            : [UIFont systemFontOfSize:16]
                               };
    NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%ld", row + 1] attributes:textAttrs];
    
    return attributedTitle;
}

@end
